﻿using CommunityToolkit.Mvvm.ComponentModel;
using Koala.Pro.Common;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using Koala.Pro.Helpers;
using Koala.Pro.Models;
using Koala.Pro.Services;
using System.ComponentModel;

namespace Koala.Pro.ViewModels
{
    public partial class OuEditViewModel : BaseEditViewModel<OuInfo, int, OuPagedDto>
    {
        [ObservableProperty]
        private List<OuInfo> deptItems = new List<OuInfo>();

        [ObservableProperty]
        private OuInfo selectedDeptItem;

        [ObservableProperty]
        private List<CListItem> userInfos = new List<CListItem>();

        [ObservableProperty]
        private List<CListItem> roleInfos = new List<CListItem>();

        public OuEditViewModel(IOuService service)
            : base(service)
        {
            base.Title = "机构（部门）信息";
            InitDictItem();
        }

        public async Task InitDictItem()
        {
            DeptItems = await SecurityHelper.GetMyDeptTree(App.ViewModel.UserInfo);
        }

        public async Task InitData(int id)
        {
            List<UserInfo> source = await BLLFactory<IUserService>.Instance.GetUsersByOU(id);
            UserInfos.Clear();
            List<CListItem> collection = source.Select((UserInfo s) => new CListItem(s.FullName + "（" + s.Name + "）", s.Id)).ToList();
            UserInfos.AddRange(collection);
            NotifyChanged("UserInfos");
            List<RoleInfo> source2 = await BLLFactory<IRoleService>.Instance.GetRolesByOu(id);
            RoleInfos.Clear();
            RoleInfos.AddRange(source2.Select((RoleInfo s) => new CListItem(s.Name + "（" + s.CompanyName + "）", s.Id)));
            NotifyChanged("RoleInfos");
        }

        protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
        }
    }
}
